package com.example.myapplication.ui.mine

import android.content.Context
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import android.widget.Toast
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.example.android_program.data_class.cinemalist_2
import com.example.myapplication.BuyTicketActivity
import com.example.myapplication.R
import com.example.myapplication.data_class.order_movie
import com.example.myapplication.ui.cinema.CinemaFragment
import com.example.myapplication.ui.movie.DateAdapter
import com.example.myapplication.ui.movie.SelectCinemaActivity
import kotlinx.android.synthetic.main.activity_cinema__choose_time_slot.*
import kotlinx.android.synthetic.main.activity_order_movie.*
import kotlinx.android.synthetic.main.fragment_cinema.*
import kotlinx.android.synthetic.main.fragment_mine.*
import okhttp3.OkHttpClient
import okhttp3.Request
import org.json.JSONArray
import java.lang.Exception
import java.lang.StringBuilder
import kotlin.concurrent.thread
import kotlin.math.log

class Order_movie_list : AppCompatActivity() {
    val order_list: ArrayList<order_movie> = ArrayList()
    val sever_ip = "192.168.43.8"
    val showAll_Order_Info_jsp = "http://${sever_ip}:8080/JspForAndroid/showAll_Order_Info.jsp"
    //val showAll_Order_Info_jsp = "http://${R.string.sever_ip}:8080/JspForAndroid/showAll_Order_Info.jsp"
    private var mRecyclerView: RecyclerView? = null
    private var mAdapter: OrderlistAapter? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_order_movie)

        Log.d("ddd", "${R.drawable.movie_1}")

        Recycler_order() //列表初始化
        Recycler_data_order()  //列表数据

        //val intent = Intent(activity, Order_movie_list::class.java)//电影
        //startActivity(intent)


    }


    fun Recycler_data_order() {
        /*order_list.add(order_movie("大地影院1","疯狂原始人1",R.drawable.movie_1,"5月5号","16:10","中文","4排6座",2,62.0))
        order_list.add(order_movie("大地影院2","疯狂原始人2",R.drawable.movie_1,"5月5号","16:10","中文","4排6座",1,32.0))
        order_list.add(order_movie("大地影院3","疯狂原始人3",R.drawable.movie_1,"5月5号","16:10","中文","4排6座",4,93.0))*/
        getAllTrainInfoJson()
    }

    private fun Recycler_order() {
        val layoutManager = LinearLayoutManager(this)
//        myrecyclerview_orider.layoutManager = layoutManager
//        val adapter2 =OrderlistAapter(order_list)
//        myrecyclerview_orider.adapter = adapter2
        mRecyclerView = findViewById(R.id.myrecyclerview_orider)
        mRecyclerView?.layoutManager = layoutManager
        mAdapter = OrderlistAapter(order_list, applicationContext)
        mRecyclerView?.adapter = mAdapter
    }

    private class OrderlistViewHolder(view: View) : RecyclerView.ViewHolder(view) {
        var order_LinearLayout: LinearLayout = view.findViewById(R.id.order_LinearLayout)
        var order_movie_Src: ImageView = view.findViewById(R.id.order_movie_Src)

        var order_movie_name: TextView = view.findViewById(R.id.order_movie_name)
        var order_number: TextView = view.findViewById(R.id.order_number)
        var order_date: TextView = view.findViewById(R.id.order_date)
        var order_time: TextView = view.findViewById(R.id.order_time)
        var order_language: TextView = view.findViewById(R.id.order_language)
        var order_cinema: TextView = view.findViewById(R.id.order_cinema)
        var order_seat: TextView = view.findViewById(R.id.order_seat)
        var order_price: TextView = view.findViewById(R.id.order_price)
    }
    //适配器
    private class OrderlistAapter(val order_list: List<order_movie>, val context: Context):
        RecyclerView.Adapter<Order_movie_list.OrderlistViewHolder>() {
        override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): Order_movie_list.OrderlistViewHolder {
            val view = LayoutInflater.from(parent.context).inflate(R.layout.layout_order_info_movie, parent, false)
            //点击事件  获取点击的  list 中的数据
            val viewHolder = OrderlistViewHolder(view)
            viewHolder.order_LinearLayout.setOnClickListener {
                val position = viewHolder.adapterPosition
                val info = order_list[position]
                //Toast.makeText(parent.context,"${info.cinema_name}-${info.movie_name}--${info.ticker_number}--${info.price}", Toast.LENGTH_LONG).show()
                val mIntent = Intent(context, OrderDetalisActivity::class.java)
                mIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK )
                mIntent.putExtra("movie_name", info.movie_name)
                mIntent.putExtra("cinema_name", info.cinema_name)
                mIntent.putExtra("date", info.date)
                mIntent.putExtra("start_time", info.time)
                mIntent.putExtra("language", info.language)
                mIntent.putExtra("price", info.price)
                mIntent.putExtra("seat", info.seat)
                Log.d("dasdsaf", "price = ${info.price}")
                context.startActivity(mIntent)
            }
            return viewHolder
        }
        override fun onBindViewHolder(holder: Order_movie_list.OrderlistViewHolder, position: Int) {
            val info = order_list[position]
            holder.order_movie_name.text = info.movie_name
            holder.order_cinema.text = info.cinema_name
            holder.order_number.text = info.ticker_number.toString()
            holder.order_date.text = info.date
            holder.order_time.text = info.time
            holder.order_language.text = info.language
            holder.order_seat.text = info.seat
            holder.order_price.text = info.price.toString()
            holder.order_movie_Src.setImageResource(info.movie_Src)
        }
        override fun getItemCount(): Int {
            return order_list.size
        }


    }

    private fun getAllTrainInfoJson(){
        thread {
            try {
                val client = OkHttpClient()

                //构建请求
                val request = Request.Builder()
                    .url(showAll_Order_Info_jsp)
                    .build()
                //执行
                val response= client.newCall(request).execute()
                //得到返回值
                val responseData = response.body?.string()
                //txtData.text = responseData
                val result = parseJson(responseData)
                if (responseData != null){
                    runOnUiThread{
                        //修改布局
                        //txtData.text = result
//                        val layoutManager = LinearLayoutManager(this)
//                        myrecyclerview_orider.layoutManager = layoutManager
//                        val adapter2 =OrderlistAapter(order_list)
//                        myrecyclerview_orider.adapter = adapter2
                        mAdapter?.notifyDataSetChanged()
                        mRecyclerView?.adapter = mAdapter
                    }
                }
            }catch (e: Exception){
                e.printStackTrace()
            }
        }
    }
    private fun parseJson(jsonData:String?):String{
        val builder = StringBuilder()
        try {
            //json数组

            val jsonArray = JSONArray(jsonData)
            //清空原来的子项
            order_list.clear()
            //向列表中加入子项
            for (i in 0 until jsonArray.length()){
                //取出元素
                val jsonObject = jsonArray.getJSONObject(i)
                val cinema_name = jsonObject.getString("cinema_name")
                val movie_name = jsonObject.getString("movie_name")
                val movie_src = jsonObject.getString("movie_src")
                val date = jsonObject.getString("date")
                val time = jsonObject.getString("time")
                val language = jsonObject.getString("language")
                val seat = jsonObject.getString("seat")
                val ticket_number = jsonObject.getInt("ticket_number")
                val price = jsonObject.getDouble("price")

                //builder.append("$movie_name--$movie_src--$movie_score--$movie_kind, ")
                order_list.add(
                    order_movie(cinema_name, movie_name, movie_src.toInt(),
                                date, time, language, seat,
                                ticket_number, price)
                )


            }
        }catch (e: Exception){
            e.printStackTrace()
        }

        return builder.toString()
    }
}